﻿using System;
using System.Collections.Generic;
using ChibiTD.Common;
using ChibiTD.Common.Enums;
using ChibiTD.Core.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ChibiTD.Core.Objects
{
    public abstract class Player : Entity, IPlayer
    {
        protected bool mShowShotArea;
        protected Color mShotAreaColor;
        protected int mShotAreaRadius;
        protected FireState mFireState;

        protected Player(Game game, int width, int height, Vector2 position) 
            : base(game, width, height, position)
        {
            this.ShowShotArea = false;
            this.ShotAreaColor = Color.White;
            this.mShotAreaRadius = 0;
            this.mFireState = FireState.None;
        }

        public bool ShowShotArea
        {
            get { return mShowShotArea; }
            set { mShowShotArea = value; }
        }

        public Color ShotAreaColor
        {
            get { return mShotAreaColor; }
            set { mShotAreaColor = value; }
        }

        public int ShotAreaRadius
        {
            get { return mShotAreaRadius; }
            set { mShotAreaRadius = value; }
        }

        public FireState FireState
        {
            get { return mFireState; }
            set { mFireState = value; }
        }

        protected Texture2D CreateShotArea()
        {
            Texture2D circle = new Texture2D(mGame.GraphicsDevice, mShotAreaRadius * 2, mShotAreaRadius * 2);
            Color[] colorData = new Color[circle.Width * circle.Height];
            int centerX = circle.Width / 2;
            int centerY = circle.Height / 2;
            
            circle.GetData<Color>(colorData);

            int i, j;
            for (i = 0; i < 2 * mShotAreaRadius; i++)
            {
                if ((centerY - mShotAreaRadius + i) >= 0 && (centerY - mShotAreaRadius + i) < circle.Height)
                {
                    int len = (int)(Math.Sqrt(Math.Cos(0.5f * Math.PI * (i - mShotAreaRadius) / mShotAreaRadius)) * mShotAreaRadius * 2);
                    int xofs = centerX - len / 2;
                    if (xofs < 0)
                    {
                        len += xofs;
                        xofs = 0;
                    }
                    if (xofs + len >= circle.Width)
                    {
                        len -= (xofs + len) - circle.Width;
                    }
                    int ofs = (centerY - mShotAreaRadius + i) * circle.Width + xofs;
                    for (j = 0; j < len; j++)
                        colorData[ofs + j] = mShotAreaColor;
                }
            }

            circle.SetData<Color>(colorData);
            return circle;
        }
    }
}
